var form, table, tableInstance, laydate;

$(function () {
    initLayUI(function () {
        initButtons();
        initOperType();
    })
});

function initLayUI(cb) {
    layui.use(['table', 'form', 'laydate'], function () {
        table = layui.table;
        form = layui.form;
        laydate = layui.laydate;

        form.render();

        tableInstance = table.render({
            elem: '#operLogTable',
            limits: [10, 15, 20],
            url: ServerURL + "/operLog/list",
            headers: tokenUtil.getAuthHeader(),
            method: "POST",
            toolbar: '#toolbar',
            cols: [[
                {type: 'checkbox'},
                {field: 'operId', title: '日志编号', align: 'center', minWidth: 100},
                {field: 'moduleName', title: '系统模块', align: 'center', minWidth: 100},
                {field: 'operType', title: '操作类型', align: 'center', minWidth: 100},
                {field: 'requestMethod', title: '请求方式', align: 'center', minWidth: 100},
                {field: 'operAccount', title: '操作账号', align: 'center', minWidth: 100},
                {field: 'operIp', title: 'IP地址', align: 'center', minWidth: 100},
                {field: 'operLocation', title: '操作地点', align: 'center', minWidth: 100},
                {field: 'operTime', title: '操作时间', align: 'center', minWidth: 100},
                {
                    field: 'isSuccess', title: '是否成功', align: 'center', minWidth: 100, templet: function (d) {
                        if (d.isSuccess) {
                            return "成功";
                        } else {
                            return "失败";
                        }
                    }
                },
                {field: 'operTime', title: '操作时间', align: 'center', minWidth: 100},
                {field: 'oper', title: '操作', minWidth: 100, align: 'center', toolbar: "#operBar"}
            ]],
            contentType: 'application/json',
            response: {
                statusName: 'code',
                statusCode: 200,
                msgName: 'msg',
                countName: 'count',
                dataName: 'data'
            },
            loading: true,
            page: true,
            error: function (res) {
                ajaxUtil.handleError(res);
            }
        });

        laydate.render({
            elem: '#operTime',
            range: ['#operBeginTime', '#operEndTime']
        });

        table.on('toolbar(operLogTable)', function (obj) {
            if (obj.event === 'delete') {
                deleteOperLog();
            } else if (obj.event === 'truncate') {
                truncateOperLog();
            }
        });

        table.on('tool(operLogTable)', function (obj) {
            if (obj.event === 'detail') {
                showDetail(obj);
            }
        });

        cb();
    });
}

function initButtons() {
    $("#searchBtn").click(function () {
        reloadTable();
    });

    $("#resetBtn").click(function () {
        form.val('form', {
            "moduleName": "",
            "operAccount": "",
            "operBeginTime": "",
            "operEndTime": "",
            "operType": "",
            "isSuccess": ""
        });
        reloadTable();
    });
}

function reloadTable() {
    let data = form.val('form');
    if (data.operBeginTime !== "") {
        data.operBeginTime = data.operBeginTime + " 00:00:00";
    }
    if (data.operEndTime !== "") {
        data.operEndTime = data.operEndTime + " 23:59:59";
    }
    tableInstance.reload({
        where: data,
        page: {
            curr: 1
        }
    });
}

function deleteOperLog() {
    let data = table.checkStatus('operLogTable').data;
    let operIds = [];
    data.forEach(function (item) {
        operIds.push(item.operId);
    });
    if (operIds.length === 0) {
        layerUtil.error("请先选中一条记录");
        return false;
    }
    layerUtil.confirm("确定要删除记录么?", function () {
        ajaxUtil.simplePost({"operIds": operIds}, "/operLog/batchDelete", function () {
            reloadTable();
        });
    });
}

function truncateOperLog() {
    layerUtil.confirm("确定要清空记录么?", function () {
        ajaxUtil.simplePost({}, "/operLog/truncate", function () {
            reloadTable();
        });
    });
}

function initOperType() {
    ajaxUtil.loadDict("sys_oper_type", function (data) {
        data.forEach(function (item) {
            $("#operType").append("<option value='" + item.dictValue + "' >" + item.dictLabel + "</option>");
        });
        form.render();
    })
}

function showDetail(obj){
    putTempData('detailOperId',obj.data.operId);
    iframeIndex = layerUtil.iframe({
        id: 'detailOperFrame',
        title: "详细信息",
        area: ['750px', '800px'],
        content: "view/log/operLogDetail.html"
    });
}